package com.mingqijia.gassafety.shared.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.gap.basic.exception.BizException;
import com.mingqijia.gassafety.shared.constant.AsstConstants;
import com.mingqijia.gassafety.shared.constant.HuiShengConstants;
import com.mingqijia.gassafety.shared.request.HuiShengCreateOrderReq;
import com.mingqijia.gassafety.shared.request.HuiShengEditOrderReq;
import com.mingqijia.gassafety.shared.request.HuiShengOrderAddRequest;
import com.mingqijia.gassafety.shared.response.HuiShengAddOrderResponse;
import com.mingqijia.gassafety.shared.response.HuiShengBaseResponse;
import com.mingqijia.gassafety.shared.service.HuiShengService;
import com.mingqijia.gassafety.shared.utils.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: duanhongxiang
 * @Desc:
 * @create: 2024-09-03 17:43
 **/
@Service
@Slf4j
public class HuiShengServiceImpl implements HuiShengService {

    @Value(value = "${huisheng.other.url}")
    String otherUrl;

    @Autowired
    HttpUtil httpUtil;

    @Override
    public HuiShengBaseResponse<HuiShengAddOrderResponse> orderAdd(HuiShengOrderAddRequest request) {
        HuiShengBaseResponse<HuiShengAddOrderResponse> huiShengBaseResponse = new HuiShengBaseResponse<>();
        huiShengBaseResponse.setErrcode(0);
        HuiShengAddOrderResponse item = new HuiShengAddOrderResponse();
        item.setOrderId(String.valueOf((new Date()).getTime()));
        item.setOrderSn(String.valueOf((new Date()).getTime()));
        huiShengBaseResponse.setData(item);
        return huiShengBaseResponse;
    }

    @Override
    public String login(String mobile) {
        Map<String, Object> params = new HashMap<>();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(date);
        String a = "appkey=55555&time=" + format;
        String sign = md5(a);
        params.put("mobile", mobile);
        params.put("sign", sign.toUpperCase());
        log.info("开始请求获取token接口：{}", JSONObject.toJSONString(params));
        String body = httpUtil.getBody(JSONObject.toJSONString(params),
                otherUrl + HuiShengConstants.LOGIN_INIT, "");
        JSONObject jsonObject = JSONObject.parseObject(body);
        if (jsonObject.get("errcode").equals(0) && !StringUtils.isEmpty(jsonObject.getJSONObject("data").getString("token"))) {
            return jsonObject.getJSONObject("data").getString("token");
        } else {
            String msg = jsonObject.get("errmsg")!=null?jsonObject.getString("errmsg"):"登录失败";
            throw new BizException(msg);
        }
    }

    @Override
    public String createOrder(HuiShengCreateOrderReq params) {
        log.info("开始请求创建师傅帮个忙工单接口：{}", JSONObject.toJSONString(params));
        String body = httpUtil.getBody(JSONObject.toJSONString(params),
                otherUrl + HuiShengConstants.ORDER_ADD, "");
        JSONObject jsonObject = JSONObject.parseObject(body);
        if (jsonObject.get("errcode").equals(0)) {
            JSONObject data = jsonObject.getJSONObject("data");
            return data.getString("order_sn");
        } else {
            String msg = jsonObject.get("errmsg")!=null?jsonObject.getString("errmsg"):"创建工单失败";
            throw new BizException(msg);
        }
    }

    @Override
    public String editOrder(HuiShengEditOrderReq params, String spId) {
        String body = httpUtil.getBody(JSONObject.toJSONString(params),
                otherUrl + HuiShengConstants.ORDER_EDIT, spId);
        JSONObject jsonObject = JSONObject.parseObject(body);
        if (jsonObject.get("errcode").equals(0)) {
            return jsonObject.getString("data");
        } else {
            String msg = jsonObject.get("errmsg")!=null?jsonObject.getString("errmsg"):"取消订单失败";
            throw new BizException(msg);
        }
    }

    @Override
    public String getOrder(Map<String, Object> params, String spId) {
        String body = httpUtil.getBody(JSONObject.toJSONString(params),
                otherUrl + HuiShengConstants.ORDER_GET, spId);
        JSONObject jsonObject = JSONObject.parseObject(body);
        if (jsonObject.get("errcode").equals(0)) {
            return jsonObject.getString("data");
        } else {
            String msg = jsonObject.get("errmsg")!=null?jsonObject.getString("errmsg"):"查询订单详情失败";
            throw new BizException(msg);
        }
    }

    @Override
    public String getlist(Map<String, Object> params) {
        String body = httpUtil.getBody(JSONObject.toJSONString(params),
                otherUrl + HuiShengConstants.ORDER_LIST, "");
        JSONObject jsonObject = JSONObject.parseObject(body);
        if (jsonObject.get("errcode").equals(0)) {
            return jsonObject.getString("data");
        } else {
            String msg = jsonObject.get("errmsg")!=null?jsonObject.getString("errmsg"):"查询订单列表失败";
            throw new BizException(msg);
        }
    }

    public static String md5(String plainString) {
        return DigestUtils.md5DigestAsHex(plainString.getBytes(StandardCharsets.UTF_8));
    }

}
